package com.sea.physical.service.impl;

import com.codecrane.core.web.BatchImportUtils;
import com.sea.physical.entity.Port;
import com.sea.physical.entity.StockProduce;
import com.sea.physical.mapper.StockProduceMapper;
import com.sea.physical.service.PortService;
import com.sea.physical.service.StockProduceService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.supply.entity.ProductPrice;
import com.sea.supply.entity.Supplier;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class StockProduceServiceImpl implements StockProduceService {

   @Autowired
   private StockProduceMapper stockProduceMapper;
   @Autowired
   private PortService portService;

    @Override
    public List<StockProduce> findByCnd(StockProduce stockProduce) {
        return stockProduceMapper.findByCnd(stockProduce);
    }

    @Override
    public int save(StockProduce stockProduce) {
        return stockProduceMapper.insert(stockProduce);
    }

    @Override
    public Integer getStockNumber(Integer stockWareId, String stockSku, Integer createNumber) {
        return stockProduceMapper.getStockNumber(stockWareId,stockSku,createNumber);
    }

    @Override
    public List<StockProduce> getUseNumber(Integer stockWareId, String stockSku) {
        return stockProduceMapper.getUseNumber(stockWareId,stockSku);
    }

    @Override
    public int updateUseNumber(Integer useNumber, Integer id) {
        return stockProduceMapper.updateUseNumber(useNumber,id);
    }

    @Override
    public int updateStockNumber(Integer useNumber, Integer id) {
        return stockProduceMapper.updateStockNumber(useNumber, id);
    }

    @Override
    public int cancelUseNumber(Integer useNumber, Integer id) {
        return stockProduceMapper.cancelUseNumber(useNumber, id);
    }

    @Override
    public List<StockProduce> findSalesProduceNumber(StockProduce stockProduce) {
        return stockProduceMapper.findSalesProduceNumber(stockProduce);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<StockProduce> list = BatchImportUtils.iteratorForStockProduce(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (StockProduce stockProduce : list) {
                    String wareName = stockProduce.getStockWareName();
                    Port port = portService.findByName(wareName);
                    if (null == port) {
                        throw new Exception("未查询到该仓库:" + wareName);
                    }

                    stockProduce.setStockWareId(port.getId());

                    StockProduce sp = findByImportData(stockProduce);
                    if(null == sp){
                        save(stockProduce);
                    }else{
                        stockProduce.setId(sp.getId());
                        updateImportUseNumber(stockProduce);
                    }
                }
            }
        }
    }

    @Override
    public StockProduce findByImportData(StockProduce stockProduce) {
        return stockProduceMapper.findByImportData(stockProduce);
    }

    @Override
    public Integer updateImportUseNumber(StockProduce stockProduce) {
        return stockProduceMapper.updateImportUseNumber(stockProduce);
    }


}
